package com.gy.assistant;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import im.zego.zim.callback.ZIMLoggedInCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.assistant.ui.MainActivity;
import com.gy.assistant.ui.setting.SettingActivity;
import com.gy.assistant.util.AppLogger;
import com.gy.assistant.util.PreferenceUtil;

import com.gy.assistant.R;
import im.zego.zpns.ZPNsManager;
import im.zego.zpns.util.ZPNsConfig;

public class LoginActivity extends AppCompatActivity {

    public static final String SP_FILE_NAME = "userinfo";
    public static final String SP_FILE_ID_KEY = "userID";
    public static final String SP_FILE_NAME_KEY = "userName";
    public static final String SP_FILE_LAST_PUSH_MODE = "push_mode";
    private static final int PUSH_MODE_FCM = 1;
    private static final int PUSH_MODE_MADE_IN_CHAIN = 2;
    public EditText userIDText;
    public EditText userNameText;

    private RadioButton pushModeChina;
    private RadioButton pushModeFcm;
    private int pushMode;
    private SharedPreferences sp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userIDText = findViewById(R.id.user_id);
        userNameText = findViewById(R.id.user_name);
        pushModeChina = findViewById(R.id.rb_push_mode_in_china);
        pushModeFcm = findViewById(R.id.rb_push_mode_fcm);

        sp = getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        String userID = sp.getString(SP_FILE_ID_KEY, null);
        String userName = sp.getString(SP_FILE_NAME_KEY, null);
        pushMode = sp.getInt(SP_FILE_LAST_PUSH_MODE, 0);
        if (userID != null) {
            userIDText.setText(userID);
            userNameText.setText(userName);
            login(userID, userName);
        }

    }

    private void registerZPNs() {
        ZPNsConfig zimPushConfig = new ZPNsConfig();
        String appType = PreferenceUtil.getInstance(this).getStringValue("appType", "0");
        if (appType != null && !appType.equals("")) {
            zimPushConfig.setAppType(Integer.parseInt(appType));
        }
        if (pushMode == PUSH_MODE_FCM) {
            zimPushConfig.enableFCMPush();
        } else if (pushMode == PUSH_MODE_MADE_IN_CHAIN) {
            zimPushConfig.enableHwBadge(true);
//            zimPushConfig.enableHWPush();
//            zimPushConfig.enableMiPush(, );
//            zimPushConfig.enableVivoPush(, );
//            zimPushConfig.enableOppoPush(, , );
        }

        if (pushMode == PUSH_MODE_FCM || pushMode == PUSH_MODE_MADE_IN_CHAIN) {
            ZPNsManager.setPushConfig(zimPushConfig);
            ZPNsManager.getInstance().registerPush(this.getApplication());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //    ZPNsManager.getInstance().unregisterPush();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String payload = "";
        String call_id = "";
        if (getIntent().getExtras() != null) {
            payload = getIntent().getExtras().getString("payload", "");
            call_id = getIntent().getExtras().getString("call_id", "");
        }
        AppLogger.getInstance().d("push payload:%s", payload);
        AppLogger.getInstance().d("push call_id:%s", call_id);
    }

    public void onLogin(View view) {
        String userID = userIDText.getText().toString();
        String userName = userNameText.getText().toString();
        if (pushModeChina.isChecked()) {
            pushMode = PUSH_MODE_MADE_IN_CHAIN;
        } else if (pushModeFcm.isChecked()) {
            pushMode = PUSH_MODE_FCM;
        }
        login(userID, userName);
    }

    private void login(String userID, String userName) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        } else {
            // 登录
            registerZPNs();
            SDKManager.sharedInstance().login(userID, userName, new ZIMLoggedInCallback() {
                @Override
                public void onLoggedIn(ZIMError zimError) {
                    if (zimError.code != ZIMErrorCode.SUCCESS) {
                        Toast.makeText(LoginActivity.this, "Login failed error info : " + zimError.code.name(), Toast.LENGTH_LONG).show();
                    } else {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString(SP_FILE_ID_KEY, userID);
                        editor.putString(SP_FILE_NAME_KEY, userName);
                        editor.putInt(SP_FILE_LAST_PUSH_MODE, pushMode);
                        editor.apply();
                        // 登录成功，进入下个页面
                        Intent intent = new Intent(getApplication(), MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            });
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        SDKManager.sharedInstance().showDebugLog(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKManager.sharedInstance().hideDebugLog(this);
    }

    public void toSetting(View view) {
        SettingActivity.actionStart(this);

    }
}
